package com.proyecto.core.vista.echo.componentes;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.table.DefaultTableModel;

import com.proyecto.core.Utilidades.UtilConversion;

import echopointng.table.AbleTableSelectionModel;
import echopointng.table.DefaultPageableSortableTableModel;
import echopointng.table.DefaultSortableTableModel;
import echopointng.table.PageableSortableTable;
import echopointng.table.PageableTableNavigation;
import echopointng.table.SortableTableModel;
import echopointng.table.SortableTableSelectionModel;

public class Ejemplo{
	
	private List list;
	
	private DefaultTableModel dtm;
	
	private DefaultPageableSortableTableModel pageableSortableTableModel;
	
	
	private SortableTableModel sortableTableModel;
	
	private SortableTableSelectionModel sortableTableSelectionModel;
	
	private PageableSortableTable tabla ;
	
	private PageableTableNavigation tableScroller;
	
	private ArrayList campos = new ArrayList();
	
	private ArrayList titulos = new ArrayList();
	
	private Object pojo = new Object();
	
	private ActionListener ManejarEvento = new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			pojo = list.get(obtenerIndice((PageableSortableTable) e.getSource()));
		}
	};
	
	public  Ejemplo(){
	}
	
	public PageableSortableTable  Ejemplo(List l){
		this.list = l;
		ArrayList al = new ArrayList();
		try {
			al = ProcesarLista(l);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		dtm = LlenarTabla(titulos, al);
		pageableSortableTableModel = new DefaultPageableSortableTableModel(dtm);
		sortableTableModel = new DefaultSortableTableModel(dtm);
		sortableTableSelectionModel = new SortableTableSelectionModel(sortableTableModel);
		pageableSortableTableModel.setRowsPerPage(10);
		AbleTableSelectionModel ableTableSelectionModel = new AbleTableSelectionModel(pageableSortableTableModel);
		ableTableSelectionModel.setSelectionMode(2);
		tabla = new PageableSortableTable(pageableSortableTableModel);
        tabla.setSelectionModel(ableTableSelectionModel);
        tabla.setSelectionEnabled(true);
        tabla.setRolloverEnabled(true);
        tabla.addActionListener(ManejarEvento);
        tableScroller = new PageableTableNavigation(tabla);
        return tabla;
	}
	
	public ArrayList ProcesarLista(List lista) throws  Exception{
		dtm = new DefaultTableModel(campos.size(),lista.size());
		ArrayList a = new ArrayList();
		Method metodo = null;
		Object obj; 
		Object o;
		for (int i = 0; i < lista.size(); i++) {
			obj = lista.get(i);
			String[] cam =new String[campos.size()];
			//Recorro cada uno de los campos del pojo
			for(int j = 0; j< campos.size(); j++){
				StringTokenizer st = new StringTokenizer(((String)campos.get(j)),".");
				String s = (String)st.nextElement();
				metodo = encontrarMetodo(obj,s );
				//Toma el atributo del pojo
				if(st.hasMoreElements()){
					o = obj;
					//Si tiene clave compuesta
					while(st.hasMoreElements()){
						o= metodo.invoke(o);
						metodo = encontrarMetodo(o, (String)st.nextElement());
						o = metodo.invoke(o);
						cam[j] =(String)o; 
					}
				//Si no tiene clave compuesta	
				}else{
					cam[j] =metodo.invoke(obj).toString();
				}
			}
			a.add(cam);
		}
		return a;
	}
	

	public Object ManejaEvento(ActionEvent e) {
		pojo = list.get(obtenerIndice((PageableSortableTable) e.getSource()));
		return pojo;
	}
	
	
	
	
	public Method encontrarMetodo(Object o, String s ){
		Method m = null;
		try {
			m = o.getClass().getMethod("get"+UtilConversion.inicialMayuscula(s));
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		return m;
	}
	
	public DefaultTableModel LlenarTabla(ArrayList titulos, ArrayList lista) {
		for (int k = 0; k < titulos.size(); k++) {
			dtm.setColumnName(k, (String) titulos.get(k));
		}
		for(int i = 0; i< lista.size();i++){
				dtm.addRow( (Object[]) lista.get(i));
		}
		return dtm;
	}
	
	public void incluirCampo(String titulo,String atributo) {
		campos.add(atributo);
		titulos.add(titulo);
	}

	public DefaultTableModel getDtm() {
		return dtm;
	}

	public int obtenerIndice(PageableSortableTable ps){
		String p=ps.getSelectionModel().toString();
		p = p.replace("(in model space)", "");
		p = p.replace(",", "");
		p= p.trim();
		return (Integer.parseInt(p)-list.size());
	}
	
	public void setDtm(DefaultTableModel dtm) {
		this.dtm = dtm;
	}

	public ActionListener getManejarEvento() {
		return ManejarEvento;
	}

	public void setManejarEvento(ActionListener manejarEvento) {
		ManejarEvento = manejarEvento;
	}

	public DefaultPageableSortableTableModel getPageableSortableTableModel() {
		return pageableSortableTableModel;
	}

	public void setPageableSortableTableModel(
			DefaultPageableSortableTableModel pageableSortableTableModel) {
		this.pageableSortableTableModel = pageableSortableTableModel;
	}

	public SortableTableModel getSortableTableModel() {
		return sortableTableModel;
	}

	public void setSortableTableModel(SortableTableModel sortableTableModel) {
		this.sortableTableModel = sortableTableModel;
	}

	public SortableTableSelectionModel getSortableTableSelectionModel() {
		return sortableTableSelectionModel;
	}

	public void setSortableTableSelectionModel(
			SortableTableSelectionModel sortableTableSelectionModel) {
		this.sortableTableSelectionModel = sortableTableSelectionModel;
	}

	public PageableSortableTable getTabla() {
		return tabla;
	}

	public void setTabla(PageableSortableTable tabla) {
		this.tabla = tabla;
	}

	public PageableTableNavigation getTableScroller() {
		return tableScroller;
	}

	public void setTableScroller(PageableTableNavigation tableScroller) {
		this.tableScroller = tableScroller;
	}
}

